import {onMounted, computed, ref} from 'vue'
import {useSys} from '@/hooks/useSys'
import {onLoad} from '@dcloudio/uni-app'

export const usePages = () => {
  const { safeArea, tabberOccupyHeight } = useSys()

  const query = reactive({})
  const pages = ref([])
  const currentPage = ref(null)
  const currentPath = ref('')
  const isFirstPage = computed(() => pages.value.length === 1)
  const tabberPageContentSafeAreaStyle = computed(() => {
    return { paddingTop: `${safeArea.top}px`, paddingBottom: `${tabberOccupyHeight.value}rpx` }
  })

  onMounted(() => {
    pages.value = getCurrentPages()
    currentPage.value = pages.value[pages.value.length - 1]
    currentPath.value = currentPage.value?.route
  })

  // 在非页面的组件内调用无效
  onLoad((option) => {
    Object.assign(query, { ...(option || {}) })
  })

  return { query, pages, currentPage, currentPath, isFirstPage, tabberPageContentSafeAreaStyle }
}
